//
//  SelectAreaController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/4.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "SelectAreaController.h"

@interface SelectAreaController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchDisplayDelegate>{
    //国际代码主tableview
    UITableView *countryCodeTableView;
  
    
    //代码字典
    NSDictionary *sortedNameDict; //代码字典
    
    NSMutableArray *indexArray;
    
    
    
}


@end

static NSInteger aaa = 1;

@implementation SelectAreaController
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //背景
    
    
    //顶部标题
    [self.titleLabel setText:StringWithKey(@"selectVC")];
    
    //创建子视图
    [self creatSubviews];
}
//创建子视图
-(void)creatSubviews{
    
    
    countryCodeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, ScreenHeight-64) style:UITableViewStylePlain];
    [self.view addSubview:countryCodeTableView];
    //自动调整自己的宽度，保证与superView左边和右边的距离不变。
    [countryCodeTableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [countryCodeTableView setDataSource:self];
    [countryCodeTableView setDelegate:self];
    [countryCodeTableView setSectionIndexBackgroundColor:[UIColor clearColor]];
    countryCodeTableView.bounces = NO;
    
    NSString *plistPathCH = [[NSBundle mainBundle] pathForResource:@"sortedChnames" ofType:@"plist"];
    NSString *plistPathEN = [[NSBundle mainBundle] pathForResource:@"sortedEnames" ofType:@"plist"];
    
    //判断当前系统语言
    if ([lanagueStyle isEqualToString:@"English"]) {
    sortedNameDict = [[NSDictionary alloc] initWithContentsOfFile:plistPathEN];
     }else{
    sortedNameDict = [[NSDictionary alloc] initWithContentsOfFile:plistPathCH];
    }
    
    
    indexArray = [[NSMutableArray alloc] initWithArray:[[sortedNameDict allKeys] sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }]];
    if ([lanagueStyle isEqualToString:@"English"]) {
        
    }else{
        NSString *remen = indexArray.lastObject;
        [indexArray removeObject:remen];
        [indexArray insertObject:remen atIndex:0];
    }
    
    
    
    
    
}


#pragma mark - UITableView
//section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [sortedNameDict allKeys].count;
    
}
//row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    NSArray *array = [sortedNameDict objectForKey:[indexArray objectAtIndex:section]];
    
    return array.count;
    
    
}
//height
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kHeight(160);
}
//初始化cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID1 = @"cellIdentifier1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID1];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID1];
    }else{
        cell = [cell initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID1];
    }
    //初始化cell数据!
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    NSString *string =  [[sortedNameDict objectForKey:[indexArray objectAtIndex:section]] objectAtIndex:row];
    NSArray *array = [string componentsSeparatedByString:@"+"];
    
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.text = array.firstObject;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"+%@", array.lastObject];
    [cell.textLabel setFont:[UIFont systemFontOfSize:16]];
    return cell;
    
}
//indexTitle
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    NSMutableArray *titleAray = [NSMutableArray arrayWithArray:indexArray];
    if (aaa == 0) {
        [titleAray removeObject:indexArray.firstObject];
        [titleAray insertObject:@"Hot" atIndex:0];
    }
    
    return titleAray;
    
}
//
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    return index;
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    return kHeight(80);
    
    
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return section == 0 ?  aaa == 0 ? @"Hot" :[indexArray objectAtIndex:section] :[indexArray objectAtIndex:section];
}

#pragma mark - 选择国际获取代码
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"选择相应国家,输出:%@==%@",cell.textLabel.text, cell.detailTextLabel.text);
    
    //    //1.代理传值
    //    if (self.deleagete && [self.deleagete respondsToSelector:@selector(returnCountryCode:)]) {
    //        [self.deleagete returnCountryCode:cell.textLabel.text];
    //    }
    //    [self dismissViewControllerAnimated:YES completion:nil];
    
    //2.block传值
    if (self.returnCountryCodeBlock != nil) {
        self.returnCountryCodeBlock(cell.detailTextLabel.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 代理传值
-(void)toReturnCountryCode:(returnCountryCodeBlock)block{
    self.returnCountryCodeBlock = block;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
